//@author A0101906R
#include "Logic.h"

using namespace Twentyfifth;
using namespace std;

const string Logic::MESSAGE_ADD = "has been added to calendar successfully.\n";
const string Logic::ERROR_UNDO = "Cannot Undo.\n";
const string Logic::ERROR_INVALID = "Invalid Command.\n";

Logic::Logic() {
	oldCmd = new CmdInvalid;
	newCmdType = Cmd::INVALID;
	oldStatusCode = Status::ERR_VOID;
}
Status Logic::executeProgram(string userInput) {
	Status status;
	string feedback;

	//put information into package
	newCmd = parserComponent.parse(userInput);
	newCmdType = newCmd->getCmdType();

	switch (newCmdType) {
	case Cmd::ADD:
	case Cmd::DEL:
	case Cmd::MARK:
	case Cmd::EDIT:
	case Cmd::GCAL:
		newCmd->setStorageMgr(&storagemgr);
		status = (*newCmd).execution();
		oldCmd = newCmd;
		oldCmd->setTask(newCmd->getTask());
		oldStatusCode = status.code;
		break;
	case Cmd::VIEW:
	case Cmd::SEARCH:
	case Cmd::EXIT:
		newCmd->setStorageMgr(&storagemgr);
		status = (*newCmd).execution();
		break;
	case Cmd::UNDO:
		status = oldCmd->undo(oldStatusCode);
		oldCmd = newCmd;
		break;
	default:
		status.code = Status::ERR_OTHER;
		status.desc = ERROR_INVALID;
		break;
	}
	return status;
}